package com.ruoyi.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.cg.pojo.Group;
import com.ruoyi.common.core.cg.pojo.Order;
import com.ruoyi.common.core.cg.pojo.Store;
import com.ruoyi.common.core.cg.pojo.User;
import com.ruoyi.common.core.cg.result.PageResult;
import com.ruoyi.common.core.cg.result.Queryparam;
import com.ruoyi.common.core.cg.result.Result;
import com.ruoyi.common.core.cg.vo.OrderVo;
import com.ruoyi.manage.mapper.*;
import com.ruoyi.manage.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 马景昌
 * @create 2025/03/09
 * @description
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private StoreMapper storeMapper;

    @Override
    public Result pageshow(Integer pageNum, Integer pageSize, Order order) {
//        if(pageNum==null && pageSize==null){
//            pageNum=1;
//            pageSize=5;
//        }
        PageHelper.startPage(pageNum, pageSize);
        List<Order> orderVos = orderMapper.selectOrderList(order);
        System.out.println(orderVos);
        for (Order vo : orderVos) {
//            用户名字
            User selectoid = userMapper.selectoid(vo.getOid());
            if (selectoid == null) {
                vo.setUserName("中间表没有添加");
            } else {
                vo.setUserName(selectoid.getNickName());
            }
//            vo.setPhone1(selectoid.getPhone());
//            商品名称
            String groupname = groupMapper.selectGroupName(vo.getOid());
            vo.setName1(groupname);
//            商家名称
            String storename = storeMapper.selectStoreName(vo.getOid());
            if (storename == null) {
                vo.setStoreName1("目前没有商家");
            } else {
                vo.setStoreName1(storename);
            }
        }
        PageInfo<Order> orderPageInfo = new PageInfo<>(orderVos);
        PageResult pageResult = new PageResult(orderPageInfo.getTotal(), orderPageInfo.getList());
        System.err.println(pageResult.getTotal());
        System.err.println(pageResult.getRows());
        return new Result(true, "查询成功", orderPageInfo);
    }

    @Override
    public Result hx(Integer oid) {
        if (oid == null) {
            return new Result(false, "参数错误");
        }
        OrderVo orderVo = orderMapper.selectbyId(oid);
//        //            用户名字  电话
//        User selectoid = userMapper.selectoid(orderVo.getOid());
//        orderVo.setUserName1(selectoid.getName());
//        orderVo.setPhone1(selectoid.getPhone());
//            商品名称
        String groupname = groupMapper.selectGroupName(orderVo.getOid());
        orderVo.setName1(groupname);
//            商家名称
        String storename = storeMapper.selectStoreName(orderVo.getOid());
        orderVo.setStoreName1(storename);
        return new Result(true, "查询成功", orderVo);
    }

    @Override
    public Result update(OrderVo order) {
        if (order == null) {
            return new Result(false, "参数错误");
        }
//        修改订单
        orderMapper.update(order);
//      先查询  修改用户名称和电话
        User selectoid = userMapper.selectoid(order.getOid());
        userMapper.updatenameorphone(selectoid.getUserId(), order.getUserName1(), order.getPhone1());
//     先查询   修改商铺名称
        Store selectid = storeMapper.selectid(order.getOid());
        storeMapper.updatename(selectid.getId(), order.getName1());

        return new Result(true, "修改成功");
    }

    @Override
    public Result delete(Integer oid) {
        orderMapper.delete(oid);
        return null;
    }

    @Override
    public Result add(Order order) {
        boolean add = orderMapper.add(order);
        if (!add) {
            return new Result(false, "添加失败");
        }
        return new Result(true, "添加成功");
    }

    @Override
    public Result itemOrder0(Integer pageNum, Integer pageSize, Order order) {
        PageHelper.startPage(pageNum, pageSize);
        List<Order> orders = orderMapper.itemOrder0(order);
        System.err.println(orders+"ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss");
        for (Order vo : orders) {
//            用户名字
            User selectoid = userMapper.selectoid(vo.getOid());
            if (selectoid == null) {
                vo.setUserName("中间表没有添加");
            } else {
                vo.setUserName(selectoid.getNickName());
            }
//            vo.setPhone1(selectoid.getPhone());
//            商品名称
            String groupname = groupMapper.selectGroupName(vo.getOid());
            vo.setName1(groupname);
//            商家名称
            String storename = storeMapper.selectStoreName(vo.getOid());
            if (storename == null) {
                vo.setStoreName1("目前没有商家");
            } else {
                vo.setStoreName1(storename);
            }
        }
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        PageResult pageResult = new PageResult(orderPageInfo.getTotal(), orderPageInfo.getList());
        System.err.println(pageResult.getTotal());
        System.err.println(pageResult.getRows());
        return new Result(true, "查询成功", orderPageInfo);
    }

    @Override
    public PageInfo<Order> orderLike(Queryparam queryparam) {
        PageHelper.startPage(queryparam.getPageNum(), queryparam.getPageSize());
        List<Order> orders = orderMapper.orderLike(queryparam);
        for (Order order : orders) {
            //            用户名字
            User selectoid = userMapper.selectoid(order.getOid());
            if (selectoid == null) {
                order.setUserName("中间表没有添加");
            } else {
                order.setUserName(selectoid.getNickName());
            }
//            vo.setPhone1(selectoid.getPhone());
//            商品名称
            String groupname = groupMapper.selectGroupName(order.getOid());
            order.setName1(groupname);
//            商家名称
            String storename = storeMapper.selectStoreName(order.getOid());
            if (storename == null) {
                order.setStoreName1("目前没有商家");
            } else {
                order.setStoreName1(storename);
            }
        }
        PageInfo<Order> pageInfo = new PageInfo<>(orders);
        return pageInfo;
    }
}
